﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LiveSequence.Common.Domain;
using LiveSequence.Suite.Documents;
using LiveSequence.Suite.Domain;
using LiveSequence.Suite.Helpers;

namespace LiveSequence.Suite.Commands
{
  public class SplitDiagramCommand : DocumentCommand<SeqDiagramDoc>
  {
    public SplitDiagramCommand(SeqDiagramDoc doc) : base(doc)
    {
      
    }

    #region Overrides
    public override string Text(object arg)
    {
      return "Split Diagram";
    }

    public override bool Execute(object arg)
    {
      MethodCallInfo methodCallInfo = arg as MethodCallInfo;

      ProjectDoc projectDoc = DocManager.Instance.MasterDocument;

      List<MethodCallInfo> lstMethods = Document.Diagram.SequenceData.GetMethodList();
      int iIndexOfMethod = lstMethods.IndexOf(methodCallInfo);
      
      LSMethodCallDiagram diagram1 = CloneHelper<LSMethodCallDiagram>.Clone(Document.Diagram);
      SeqDiagramDoc seqDiagramDoc1 = new SeqDiagramDoc(diagram1);

      LSMethodCallDiagram diagram2 = CloneHelper<LSMethodCallDiagram>.Clone(Document.Diagram);
      SeqDiagramDoc seqDiagramDoc2 = new SeqDiagramDoc(diagram2);

      List<MethodCallInfo> lstMethods1 = diagram1.SequenceData.GetMethodList();
      List<MethodCallInfo> lstMethods2 = diagram2.SequenceData.GetMethodList();
      for (int iIndex = 0; iIndex < lstMethods.Count; iIndex++)
      {
        if(iIndex<iIndexOfMethod)
        {
          lstMethods1[iIndex].Visible = false;
        }
        else
        {
          lstMethods2[iIndex].Visible = false;
        }
      }


      if (projectDoc != null)
      {
        projectDoc.AddDiagram(seqDiagramDoc1);
        projectDoc.AddDiagram(seqDiagramDoc2);
      }

      DocManager.Instance.ActiveDocument = seqDiagramDoc1;

      return true;
    }

    public override bool CanExecute(object arg)
    {
      return true;
    }

    #endregion
  }
}
